package com.example.administrator.familfinalcialmanagement.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.bean.GreenDaoUtils;
import com.example.administrator.familfinalcialmanagement.bean.ReasionBean;
import com.example.administrator.familfinalcialmanagement.databinding.ActivityMyInfoBinding;
import com.example.administrator.familfinalcialmanagement.greendao.gen.ReasionBeanDao;
import com.example.administrator.familfinalcialmanagement.ui.BaseActivity;
import com.example.administrator.familfinalcialmanagement.util.GlideUtil;
import com.example.administrator.familfinalcialmanagement.util.ImageUtils;
import com.example.administrator.familfinalcialmanagement.util.LogUtil;
import com.example.administrator.familfinalcialmanagement.util.PermissionUtil;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;
import com.example.administrator.familfinalcialmanagement.util.dialog.PhotoDialog;
import com.example.administrator.familfinalcialmanagement.util.listener.OnBHClickListener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

import static com.example.administrator.familfinalcialmanagement.Constants.LOGINPRESSION;
import static com.example.administrator.familfinalcialmanagement.Constants.REQUEST_CODE_LOCATION;

/**
 * Created by Administrator on 2019/3/26.
 */

public class MyInfoActivity extends BaseActivity {

    ActivityMyInfoBinding binding;
    //0 男  1 女
    String sex = "0";
    Uri uri;
    String capath;
    List<File> files = new ArrayList<>();

    ReasionBeanDao dao = GreenDaoUtils.getSingleTon().getmDaoSession().getReasionBeanDao();
    ReasionBean bean;
    String type,className;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_my_info);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        setBack();

        type = getIntent().getStringExtra( "module_type" );
        className = getIntent().getStringExtra("module_class");
        LogUtil.e("接受type-->"+ type);
        LogUtil.e("接受className-->"+ className);
        setTitleText(type);

        if (type.equals( "teacher" )) {
            binding.llModuleClassName.setVisibility( View.VISIBLE );
            binding.llModuleTime.setVisibility( View.VISIBLE );
            binding.llModuleEB.setVisibility( View.VISIBLE );
        }

        /**
         * 如果传输的className是空，则属于我是学生/老师
         * 如果非空，则属于热门课程跳转
         */
        if (TextUtils.isEmpty(className)) {
            for(ReasionBean reasionBean: dao.loadAll()){
                if (reasionBean.getJob().equals( type )) {
                    bean = reasionBean;
                    binding.setBean( bean );
                    LogUtil.e( "角色-->"+ bean.getJob() );
                }
            }
        }else{
            for(ReasionBean reasionBean: dao.loadAll()){
                if (reasionBean.getJob().equals( type ) && reasionBean.getClassName().equals(className)) {
                    bean = reasionBean;
                    binding.setBean( bean );
                    LogUtil.e( "角色-->"+ bean.getJob() );
                }
            }
        }

        binding.tvModuleUp.setOnClickListener(this);
        binding.ivModuleHead.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ivModuleHead:
                PhotoDialog dialog = new PhotoDialog(this);
                dialog.show();
                dialog.setOnBHClickListener(new OnBHClickListener() {
                    @Override
                    public void onConfirmClick() {
                        photoPoint();
                    }

                    @Override
                    public void onCloseClick() {
                        photoAlbum();
                    }
                });
                break;
            default:
        }
    }

    //照相
    public void photoPoint() {
        if (PermissionUtil.cameraPermission(this, LOGINPRESSION)) {
            uri = ImageUtils.createImagePathUri(this);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            startActivityForResult(intent, 0);
        }
    }

    //选择相册图片
    public void photoAlbum() {
        if (PermissionUtil.cameraPermission(this, REQUEST_CODE_LOCATION)) {
            Intent intent = new Intent("android.intent.action.PICK");
            intent.setType("image/*");
            startActivityForResult(intent, 3);
        }
    }

    //动态请求权限的响应
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case LOGINPRESSION:
                if (PermissionUtil.loginPermission(this, LOGINPRESSION)) {
                    photoPoint();
                } else {
                    ToastUtil.showToast("请允许相关权限");
                    return;
                }
                break;
            case REQUEST_CODE_LOCATION:
                if (PermissionUtil.loginPermission(this, REQUEST_CODE_LOCATION)) {
                    photoAlbum();
                } else {
                    ToastUtil.showToast("请允许相关权限");
                    return;
                }
                break;
            default:
        }
    }

    //选择图片返回
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == 0) {
                capath = uri + "";
                if ((capath + "").contains("content")) {
                    capath = ImageUtils.getRealPathFromURI(this, uri);
                } else if (capath.contains("file:///")) {
                    capath = capath.substring(7, capath.length());
                } else {
                    capath = data.getData().toString();
                }
                lubanMethod(capath);
                GlideUtil.setCircleMethod(MyInfoActivity.this, capath, binding.ivModuleHead);
            } else if (requestCode == 3) {
                if (data == null) {
                    return;
                }
                capath = ImageUtils.getInstance().getFilePathMethod(this, data.getData());
                lubanMethod(capath);
                GlideUtil.setCircleMethod(MyInfoActivity.this, capath, binding.ivModuleHead);
            }
        }
    }

    public void lubanMethod(String str) {
        Luban.with(this).
                load(new File(str)).
                setCompressListener(new OnCompressListener() {
                    @Override
                    public void onStart() {
                        files.clear();
                    }

                    @Override
                    public void onSuccess(File file) {
                        files.add(file);
                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtil.e("鲁班压缩出错");
                    }

                }).launch();
    }


}
